﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraTreeList.ViewInfo;
using DevExpress.XtraTreeList;
using System.Collections;
using System.Drawing;
using System.Reflection;
using DevExpress.Utils;
using DTD2013.Common;

namespace DTD2013.DTDComponent
{
    public class DTDTreeListViewInfo : TreeListViewInfo
    {
        public DTDTreeListViewInfo(TreeList fTreeList) : base(fTreeList) { }

        protected override void CalcRowCellsInfo(RowInfo ri, ArrayList viewInfoList)
        {
            base.CalcRowCellsInfo(ri, viewInfoList);
            if (ri.Node.Level == TreeListLevel.NhomCV)
            {
                // remove lines
                for (int i = ri.Lines.Count - 1; i >= 0; i--)
                    if (IsVerticalLine(ri.Lines[i])) ri.Lines.RemoveAt(i);

                // remove cells
                if (ri.Cells.Count > 1)
                {
                    CellInfo firstCell = (CellInfo)ri.Cells[1];
                    CellInfo lastCell = (CellInfo)ri.Cells[ri.Cells.Count - 1];
                    Rectangle bounds = new Rectangle(firstCell.Bounds.Location, new Size(lastCell.Bounds.Right - firstCell.Bounds.Left, firstCell.Bounds.Height));
                    firstCell.CalcViewInfo(GInfo.Graphics, Point.Empty, bounds);
                    bounds.Inflate(-1, -1);
                    firstCell.EditorViewInfo.Bounds = bounds;
                    ri.Cells.Clear();
                    ri.Cells.Add(firstCell);
                }
            }
        }

        protected override TreeNodeCellState CalcRowCellState(CellInfo cell)
        {
            TreeNodeCellState state = base.CalcRowCellState(cell);
            if (cell.RowInfo.Node.HasChildren && cell.RowInfo.Node == TreeList.FocusedNode)
                state |= TreeNodeCellState.FocusedCell;
            return state;
        }

        private bool IsVerticalLine(object line)
        {
            FieldInfo fi = line.GetType().GetField("Appearance", BindingFlags.Instance | BindingFlags.Public);
            AppearanceObject app = (AppearanceObject)fi.GetValue(line);
            return (app == PaintAppearance.VertLine);
        }
    }
}
